package push.tools.service.service.egova;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import push.tools.service.config.network.OkHttpClientConfig;
import push.tools.service.constant.ApiUrl;
import push.tools.service.entity.CaseBean;

import java.util.StringJoiner;

@Service
public class SupportCaseService {
    private static final Logger logger = LoggerFactory.getLogger(SupportCaseService.class);

    @Autowired
    public OkHttpClient okHttpClient;

    public void replenish(CaseBean caseBean) {
        if (caseBean == null || caseBean.eventcode == null || caseBean.eventcode.isEmpty()) {
            return;
        }
        String eventCode = caseBean.eventcode;
        String reqUrl = ApiUrl.supportDispose + "?taskNum=" + eventCode;
        Request request = OkHttpClientConfig.getRequest(reqUrl);
        try (Response response = okHttpClient.newCall(request).execute()) {
            String respString = response.body().string();
            JSONObject jsonObject = (JSONObject) JSON.parse(respString);
            JSONObject data = (JSONObject) jsonObject.get("data");
            if (data == null) {
                return;
            }
            JSONObject recInfo = (JSONObject) data.get("recInfo");
            if (recInfo == null) {
                return;
            }
            //开始对CaseBean补充复制
            caseBean.currentDeptName = recInfo.getString("disposePerson");//当前处理部门
            caseBean.currentDealPeopleName = recInfo.getString("actDefName");//当前处理人
            JSONArray medias = recInfo.getJSONArray("medias");
            StringJoiner stringJoiner = new StringJoiner(",");//图片信息
            for (Object media : medias) {
                JSONObject o = (JSONObject) media;
                String s = o.getString("mediaURL").replaceAll("null", ApiUrl.picHost);
                stringJoiner.add(s);
            }
            caseBean.fileUrls = stringJoiner.toString();
        } catch (Exception e) {
            throw new RuntimeException("任务号:" + eventCode + ",获取英德城管案件详情失败:" + e.getMessage());
        }
    }
}
